/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using Core.Model.Data.Generics;
using Core.Model.Data.Type;

namespace Core.Model.Data
{
    public class Content
    {
        private String textContent;

        public String TextContent
        {
            get { return textContent; }
            set
            {
                if (value == null) throw new NullReferenceException("Setting to Null Value");
                textContent = value;
                bitmapContent = null;
                
            }
        }

        private Image bitmapContent;

        public Image BitmapContent
        {
            get { return bitmapContent; }
            set
            {
                if (value == null) throw new NullReferenceException("Setting to Null Value");
                bitmapContent = value;
                textContent = null;
              
            }
        }

        private DirectoryInfo contentRoot;

        public DirectoryInfo ContentRoot
        {
            get { return contentRoot; }
            set { contentRoot = value; }
        }

        private GenericContentRef reference;

        public GenericContentRef Reference
        {
            get { return reference; }
            set { reference = value; }
        }

        private int? contentSize = null;

        public int? ContentSize
        {
            get { return contentSize; }
            set { contentSize = value; }
        }

        private int? contentCachePriority = null;

        public int? ContentCachePriority
        {
            get { return contentCachePriority; }
            set { contentCachePriority = value; }
        }

        #region Constructors

        public Content ()
        {
        }

        public Content ( String textContent )
        {
            if (textContent== null) throw new NullReferenceException("Setting to Null Value");
            this.textContent = textContent;
            bitmapContent = null;
            reference = null;
        }

        public Content ( Image bitmapContent )
        {
            if (bitmapContent == null) throw new NullReferenceException("Setting to Null Value");
            this.bitmapContent = bitmapContent;
            textContent = null;
            reference = null;
        }

        public Content ( GenericContentRef genericContentRef)
        {
            if (genericContentRef == null) throw new NullReferenceException("Setting to Null Value");
            textContent = null;
            bitmapContent = null;
            this.reference = genericContentRef;
        }
        
        public Content(ContentRef contentRef)
        {
            if (contentRef == null) throw new NullReferenceException("Setting to Null Value");
            textContent = null;
            bitmapContent = null;
            this.reference = new GenericContentRef(contentRef);
        }


        public Content ( object content )
        {
            if (content == null) throw new NullReferenceException("Setting to Null Value");
            
            if ( content is ContentRef )
            {
                textContent = null;
                bitmapContent = null;
                this.reference = new GenericContentRef( (ContentRef)content );

            }
            else
            {
                this.textContent =  (String)content;
                bitmapContent = null;
                reference = null;
            }

        }

 
        #endregion


        public object Data()
        {
            if (textContent != null)
                return textContent;
            else if (bitmapContent != null)
                return bitmapContent;
            else
                throw new NullReferenceException("Should be either text or bitmap");

        }

        public bool IsBitmap ()
        {
            return bitmapContent!=null;
        }

        public bool IsText ()
        {
            return textContent!=null;
        }

        public bool IsReference ()
        {
            return reference != null;
        }

        public bool IsDirect ()
        {
            return reference == null;
        }

        public bool IsEmpty ()
        {
            return (textContent == null || (textContent != null && textContent =="" )) && bitmapContent == null;
        }

        #region ICloneable Members

        public Content Clone ()
        {
            Content clone = null;

            if ( IsDirect() )
            {
                if ( IsText() )
                    clone = new Content( textContent );
                else if ( IsBitmap() )
                    clone = new Content( BitmapContent );

            }
            else
                clone = new Content( reference );

            clone.ContentSize = this.ContentSize;
            clone.ContentCachePriority = this.ContentCachePriority;

            return clone;


        }

        #endregion
    }
}
